package snmptest.discover;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.isnmp.common.snmp.SNMPFactory;
import com.isnmp.common.snmp.SNMPTarget;
import com.isnmp.common.snmp.mib.MibIfEntry;
import com.isnmp.common.snmp.mib.MibMacIP;
import com.isnmp.common.snmp.mib.MibSoftwareRunEntry;
import com.isnmp.nms.framework.discover.DeviceInfo;
import com.isnmp.nms.framework.discover.DeviceTypeInfo;
import com.isnmp.nms.framework.discover.DiscoverAPI;
import com.isnmp.nms.framework.discover.DiscoverConfig;
import com.isnmp.nms.framework.discover.LinkInfo;
import com.isnmp.nms.framework.discover.ResourceInfo;
import com.isnmp.nms.framework.discover.ResourceSearchMothType;
import com.isnmp.nms.framework.discover.rstype.CPUType;
import com.isnmp.nms.framework.discover.rstype.MemoryType;

public class DiscoverTest extends Thread {
    static {
        //------------------------------------------------
        DeviceTypeInfo type = new DeviceTypeInfo();
        type.setDeviceProductType("Windows 2003");
        type.setLogicType("计算机");
        List<String> oids = new ArrayList<String>();
        oids.add("1.3.6.1.4.1.311.1.1.3.1.3.1.2");
        type.setTypeOIDCharacter(oids);
        DiscoverConfig.addDeviceType(type);

        DeviceTypeInfo type1 = new DeviceTypeInfo();
        type.setDeviceProductType("HP服务器");
        type.setLogicType("计算机");
        List<String> oids1 = new ArrayList<String>();
        oids.add("1.3.6.1.4.1.11.2.3.2.3");
        type.setTypeOIDCharacter(oids1);
        DiscoverConfig.addDeviceType(type1);

        DeviceTypeInfo type2 = new DeviceTypeInfo();
        type.setDeviceProductType("Cisco C2600");
        type.setLogicType("路由交换机");
        List<String> oids2 = new ArrayList<String>();
        oids.add("192.168.174.12");
        type.setTypeOIDCharacter(oids2);
        DiscoverConfig.addDeviceType(type2);

        DeviceTypeInfo type3 = new DeviceTypeInfo();
        type.setDeviceProductType("Windows计算机");
        type.setLogicType("计算机");
        List<String> descs = new ArrayList<String>();
        descs.add("windows");
        type.setTypeDescCharacter(descs);
        DiscoverConfig.addDeviceType(type3);

        DeviceTypeInfo type4 = new DeviceTypeInfo();
        type.setDeviceProductType("IBM AIX服务器");
        type.setLogicType("计算机");
        List<String> descs4 = new ArrayList<String>();
        descs.add("AIX");
        type.setTypeDescCharacter(descs4);
        DiscoverConfig.addDeviceType(type4);

        DeviceTypeInfo type5 = new DeviceTypeInfo();
        type.setDeviceProductType("H3C路由器");
        type.setLogicType("路由器");
        List<String> descs5 = new ArrayList<String>();
        descs.add("H3C Series Router");
        type.setTypeDescCharacter(descs5);
        DiscoverConfig.addDeviceType(type5);

        //-----------------------------------------------------------

        ResourceSearchMothType type6 = new CPUType();
        DiscoverConfig.addResourceType(type6);

        //        ResourceSearchMothType type7 = new InterfaceType();
        //        DiscoverConfig.addResourceType(type7);
        //

        ResourceSearchMothType type8 = new MemoryType();
        DiscoverConfig.addResourceType(type8);

        //        ResourceSearchMothType type9 = new SoftwareRunType();
        //        DiscoverConfig.addResourceType(type9);

        //        ResourceSearchMothType type10 = new GernalGroupSearchType();
        //        type10.setResouceBaseType(ResourceSearchMothType.ResouceBaseTypeSNMPGroup);
        //        type10.setResourceRSID("1.3.6.1.2.1.1.1.0");
        //        type10.setResoureTypeName("SNMPSystem");
        //        type10.setDisplayNameCode("系统名:<1.3.6.1.2.1.1.5.0>");
        //        type10.setParamCode("位置名:<1.3.6.1.2.1.1.5.0>");
        //        type10.setTypeDesc("类型描述");
        //        DiscoverConfig.addResourceType(type10);

        //        ResourceSearchMothType type11 = new GernalTableSearchType();
        //        type11.setResouceBaseType(ResourceSearchMothType.ResouceBaseTypeSNMPTable);
        //        type11.setResourceRSID("1.3.6.1.2.1.2.2.1.2");
        //        type11.setResoureTypeName("SNMP接口");
        //        type11.setDisplayNameCode("接口desc:<1.3.6.1.2.1.2.2.1.2>");
        //        type11.setParamCode("接口编号:<1.3.6.1.2.1.2.2.1.1>");
        //        type11.setTypeDesc("接口类型描述");
        //        DiscoverConfig.addResourceType(type11);

        //------------------------------------------------------------
    }

    public static void main(String[] args) {
        System.out.println("开始时间：" + new Timestamp(System.currentTimeMillis()).toString());
        //initSNMP();
        initSNMP();
        //testSeachByRoamingLink();
        //testPhysicsLink();

        //testSearchDeviceByIP();
        //testSearchDeviceByNet();
        //        testSearchResource();
        //        testPhysicsLink();

        String str = "file '" + 12 + "'";
        System.out.println(str);
        testss();

        //        testSearchDeviceByIP();

        System.out.println("本搜索全部结束!!!!!!结束时间：" + new Timestamp(System.currentTimeMillis()).toString());
        System.exit(0);
    }

    static void initSNMP() {
        try {
            SNMPFactory.init(1, null, 8088);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 测试通过一个核心网络设备发现网络资源
     */
    public static void testSearchDeviceByNet() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = 0;
            snmpTargetList.add(target2);

            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDeviceByNetList(getLargTestNet(), snmpTargetList, false);
            for (int i = 1; i < 10000; i++) {
                deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("172.168.0.246", snmpTargetList, false);
            }
            System.out.println("搜索到设备的个数:" + deviceList.size());
            for (DeviceInfo device : deviceList) {
                System.out.println(device.getDeviceName());
                System.out.println(device.getDeviceDesc());
                System.out.println(device.getDeviceIP());
                System.out.println(device.getDeviceMAC());
                System.out.println(device.getDeviceProtocol());
                System.out.println(device.getDeviceSysOID());
                System.out.println(device.getDeviceType().getDeviceProductType());
                System.out.println("-----------------");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 测试发现一个IP段内的网络资源
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static void testSearchDeviceByIP() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            target1.snmpVersion = 1;
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = 2;
            snmpTargetList.add(target2);
            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("172.168.1.1", "172.168.1.250", snmpTargetList, true);

            System.out.println("搜索到设备的个数:" + deviceList.size());
            for (DeviceInfo device : deviceList) {
                System.out.println(device.getDeviceName());
                System.out.println(device.getDeviceDesc());
                System.out.println(device.getDeviceIP());
                System.out.println("MAC:" + device.getDeviceMAC());
                System.out.println(device.getDeviceProtocol());
                System.out.println(device.getDeviceSysOID());
                System.out.println(device.getDeviceType().getDeviceProductType());
                System.out.println("-----------------");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 测试通过IP路由发现网络设备
     */
    public static void testSeachByIPRoaming() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = 0;
            snmpTargetList.add(target2);

            List nets = new Vector();
            nets.add("192.168.1.0");

            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDeviceByIPRoaming(nets, snmpTargetList, true, 6, 800);
            System.out.println("从本网络和漫游网络搜索到的设备个数:" + deviceList.size());
            for (DeviceInfo device : deviceList) {
                System.out.println(device.getDeviceName());
                System.out.println(device.getDeviceDesc());
                System.out.println(device.getDeviceIP());
                System.out.println(device.getDeviceMAC());
                System.out.println(device.getDeviceProtocol());
                System.out.println(device.getDeviceSysOID());
                System.out.println(device.getDeviceType().getDeviceProductType());
                System.out.println("-----------------");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 测试网络设备之间的连接
     */
    public static void testSeachByRoamingLink() {
        try {

            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            target1.snmpVersion = SNMPTarget.VERSION1;
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = SNMPTarget.VERSION2C;
            snmpTargetList.add(target2);
            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("172.168.0.1", "172.168.0.254", snmpTargetList, true);

            System.out.println("漫游搜索设备个数:" + deviceList.size());
            for (DeviceInfo device : deviceList) {
                System.out.println(device.getDeviceName());
                System.out.println(device.getDeviceDesc());
                System.out.println(device.getDeviceIP());
                System.out.println(device.getDeviceMAC());
                System.out.println(device.getDeviceProtocol());
                System.out.println(device.getDeviceSysOID());
                System.out.println(device.getDeviceType().getDeviceProductType());
                System.out.println("------------------------------------");
            }

            List<LinkInfo> linkList = DiscoverAPI.getDiscoverAPI().searchLinkInfo(deviceList);

            System.out.println("发现的设备连接数：" + linkList.size());
            if (linkList != null) {
                for (LinkInfo link : linkList) {
                    System.out.println("************************");
                    DeviceInfo startDevice = link.startDevice;
                    DeviceInfo endDevice = link.endDevice;
                    if (startDevice != null) {
                        System.out.println("连接起点:" + startDevice.getDeviceIP() + "名称:" + startDevice.getDeviceName() + "类型:" + startDevice.getDeviceType().getLogicType() + ",端口：" + link.startPort);
                    }
                    if (endDevice != null) {
                        System.out.println("连接终点:" + endDevice.getDeviceIP() + "名称:" + endDevice.getDeviceName() + "类型:" + endDevice.getDeviceType().getLogicType() + ",端口: " + link.endPort);
                    }
                }
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void testSearchNetByRouter() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = 0;
            snmpTargetList.add(target2);

            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("192.168.174.0", snmpTargetList, true);
            System.out.println("搜索到本网络和路由网络的设备个数:" + deviceList.size());
            for (DeviceInfo device : deviceList) {
                System.out.println(device.getDeviceName());
                System.out.println(device.getDeviceDesc());
                System.out.println(device.getDeviceIP());
                System.out.println(device.getDeviceMAC());
                System.out.println(device.getDeviceProtocol());
                System.out.println(device.getDeviceSysOID());
                System.out.println(device.getDeviceType().getDeviceProductType());
                System.out.println("-----------------");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void testPhysicsLink() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            target1.snmpVersion = 1;
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = 2;
            snmpTargetList.add(target2);

            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("172.168.1.1", "172.168.1.254", snmpTargetList, true);
            MibMacIP mibMacIP;
            for (DeviceInfo device : deviceList) {
                System.out.println("设备：" + device);
                System.out.println("描述:" + device.getDeviceDesc());
                System.out.println("主MAC地址:" + device.getDeviceMAC());
                System.out.println("网管协议:" + device.getDeviceProtocol());
                System.out.println("设备OID标识：" + device.getDeviceSysOID());
                System.out.println("此设备具有的MAC地址:");
                for (String mac : device.getDeviceMacList()) {
                    System.out.println("Mac:" + mac);
                }
                System.out.println("此设备的网络接口信息:");
                for (MibIfEntry mibIf : device.getIfTableList()) {
                    System.out.println(mibIf);
                }
                System.out.println("此设备的ARP信息(MAC与IP学习表):");
                for (Iterator ite = device.getMibMacIPList().iterator(); ite.hasNext();) {
                    mibMacIP = (MibMacIP) ite.next();

                    System.out.println("ARP——" + mibMacIP + ",所在接口:" + mibMacIP.getIpNetToMediaIfIndex());
                }
                System.out.println("------------------");
            }

            List<LinkInfo> linkList = DiscoverAPI.getDiscoverAPI().searchLinkInfo(deviceList);
            for (LinkInfo link : linkList) {
                System.out.println(link);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void testLinkBySimulation() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            target1.snmpVersion = 1;
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "publicxxx";
            target2.snmpVersion = 1;
            snmpTargetList.add(target2);

            List<DeviceInfo> deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("8.8.8.1", "8.8.8.16", snmpTargetList, false);
            String mac;
            for (DeviceInfo device : deviceList) {
                System.out.println(device);
                System.out.println(device.getDeviceDesc());
                System.out.println(device.getDeviceMAC());
                System.out.println(device.getDeviceProtocol());
                System.out.println(device.getDeviceSysOID());
                System.out.println("此设备具有的MAC地址:");
                for (Iterator localIterator2 = device.getDeviceMacList().iterator(); localIterator2.hasNext();) {
                    mac = (String) localIterator2.next();

                    System.out.println("Mac:" + mac);
                }
                System.out.println("------------------");
            }

            List<LinkInfo> linkList = DiscoverAPI.getDiscoverAPI().searchLinkInfo(deviceList);
            for (LinkInfo link : linkList) {
                System.out.println(link);
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 获取设备的资源信息
     */
    public static void testSearchResource() {
        try {
            DeviceInfo device = new DeviceInfo();
            device.setDeviceIP("172.168.1.82");
            device.setDeviceProtocol("SNMP");
            SNMPTarget target = new SNMPTarget();
            target.nodeIP = "172.168.1.82";
            device.setSnmpTarget(target);
            List<ResourceInfo> rsList = DiscoverAPI.getDiscoverAPI().searchDeviceResource(device);
            System.out.println("发现资源个数:" + rsList.size());
            for (ResourceInfo resource : rsList) {
                System.out.println("资源名：" + resource.getResourceName());
                System.out.println("residvalue:" + resource.getResourceRSIDValue());
                System.out.println("资源类型：" + resource.getResourceTypeName());
                System.out.println("资源参数：" + resource.getResourceParam());
                System.out.println("资源描述:" + resource.getResourceDesc());
                System.out.println("资源备注:" + resource.getResrouceRemarks());
                System.out.println("---------------------------------");
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public static void testSearchDeviceAndResource() {
        try {
            List snmpTargetList = new Vector();
            SNMPTarget target1 = new SNMPTarget();
            target1.port = 161;
            target1.readCommunity = "public";
            snmpTargetList.add(target1);

            SNMPTarget target2 = new SNMPTarget();
            target2.port = 161;
            target2.readCommunity = "public";
            target2.snmpVersion = 0;
            snmpTargetList.add(target2);

            List deviceList = DiscoverAPI.getDiscoverAPI().searchDevice("127.0.0.0", snmpTargetList, false);
            System.out.println("搜索到本网络和路由网络的设备个数:" + deviceList.size());
            Iterator localIterator1;
            for (;; localIterator1.hasNext()) {
                localIterator1 = deviceList.iterator();
                //                continue;
                DeviceInfo device = (DeviceInfo) localIterator1.next();

                List rsList = DiscoverAPI.getDiscoverAPI().searchDeviceResource(device);
                System.out.println("发现资源个数:" + rsList.size());
                ResourceInfo localResourceInfo;
                for (Iterator localIterator2 = rsList.iterator(); localIterator2.hasNext(); localResourceInfo = (ResourceInfo) localIterator2.next())
                    ;
                System.out.println("=================================================");
            }

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    /**
     * 测试性能
     */
    @SuppressWarnings("unchecked")
    public static void testss() {
        try {
            SNMPTarget target = new SNMPTarget();
            target.nodeIP = "127.0.0.1";
            target.readCommunity = "public";
            //            List<Dot1dTpFdbEntry> list = SNMPFactory.getSNMPAPI().getAllTableData(Dot1dTpFdbEntry.class, target);
            //            System.out.println(list.size());
            //            for (Dot1dTpFdbEntry dot : list) {
            //                System.out.println(dot.toString());
            //            }
            //
            //            List<MibIfEntry> list2 = SNMPFactory.getSNMPAPI().getAllTableData(MibIfEntry.class, target);
            //            System.out.println(list2.size());
            //            for (MibIfEntry ifEntity : list2) {
            //                System.out.println(ifEntity.toString());
            //            }
            //
            //            List<MibIPAddrEntry> list3 = SNMPFactory.getSNMPAPI().getAllTableData(MibIPAddrEntry.class, target);
            //            System.out.println(list3.size());
            //            for (MibIPAddrEntry ipAddr : list3) {
            //                System.out.println(ipAddr.toString());
            //            }
            //            List<MibDeviceEntry> list4 = SNMPFactory.getSNMPAPI().getAllTableData(MibDeviceEntry.class, target);
            //            System.out.println(list4.size());
            //            for (MibDeviceEntry ipAddr : list4) {
            //                System.out.println(ipAddr.toString());
            //            }
            //            List<MibDiskAndMemoEntry> list5 = SNMPFactory.getSNMPAPI().getAllTableData(MibDiskAndMemoEntry.class, target);
            //            System.out.println(list5.size());
            //            for (MibDiskAndMemoEntry ipAddr : list5) {
            //                System.out.println(ipAddr.toString());
            //            }
            List<MibSoftwareRunEntry> list5 = SNMPFactory.getSNMPAPI().getAllTableData(MibSoftwareRunEntry.class, target);
            System.out.println(list5.size());
            for (MibSoftwareRunEntry ipAddr : list5) {
                System.out.println(ipAddr.toString());
            }
            //            List<MibSoftwareInstallEntry> list5 = SNMPFactory.getSNMPAPI().getAllTableData(MibSoftwareInstallEntry.class, target);
            //            System.out.println(list5.size());
            //            for (MibSoftwareInstallEntry ipAddr : list5) {
            //                System.out.println(ipAddr.toString());
            //            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private static List<String> getLargTestNet() {
        List<String> netList = new ArrayList<String>();
        netList.add("192.168.8.0");
        netList.add("192.168.1.0");
        netList.add("192.168.2.0");
        netList.add("192.168.3.0");
        netList.add("192.168.4.0");
        netList.add("192.168.5.0");
        netList.add("192.168.6.0");
        netList.add("192.168.7.0");
        netList.add("192.168.8.0");
        netList.add("192.168.9.0");
        netList.add("192.168.10.0");
        netList.add("192.168.11.0");
        netList.add("192.168.12.0");
        netList.add("192.168.13.0");
        netList.add("192.168.14.0");
        netList.add("192.168.15.0");
        netList.add("192.168.16.0");
        netList.add("192.168.17.0");
        netList.add("192.168.18.0");
        netList.add("192.168.19.0");
        netList.add("192.168.20.0");
        netList.add("192.168.21.0");
        netList.add("192.168.22.0");
        netList.add("192.168.23.0");
        netList.add("192.168.24.0");
        netList.add("192.168.25.0");
        netList.add("192.168.26.0");
        netList.add("192.168.27.0");
        netList.add("192.168.28.0");
        netList.add("192.168.29.0");
        netList.add("192.168.30.0");
        netList.add("192.168.31.0");
        netList.add("192.168.32.0");
        netList.add("192.168.33.0");
        netList.add("192.168.34.0");
        netList.add("192.168.35.0");
        netList.add("192.168.36.0");
        netList.add("192.168.37.0");
        netList.add("192.168.38.0");
        netList.add("192.168.39.0");
        netList.add("192.168.40.0");
        netList.add("192.168.41.0");
        netList.add("192.168.42.0");
        netList.add("192.168.43.0");
        netList.add("192.168.44.0");
        netList.add("192.168.45.0");
        netList.add("192.168.46.0");
        netList.add("192.168.47.0");
        netList.add("192.168.48.0");
        netList.add("192.168.49.0");
        netList.add("192.168.50.0");
        return netList;
    }
}